package com.example.blog;

import com.example.blog.Common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Slf4j
public class TaskService {
    @Autowired
    private RedisTemplate redisTemplate;
    @PostConstruct
    public void init(){
        log.info("启动初始化");
        //定时5s，模拟微博的热度刷新(比如说模拟点赞，收藏，评论)
        new Thread(()->this.refreshDataHour()).start();
        //定时一小时，合并统计天，周，月的排行榜
        new Thread(()->this.refreshData()).start();
    }
    //采用26个英文字母来实现排行，随机为每个字母生成一个随机数作为score
    public void refreshHour(){
        long hour=System.currentTimeMillis()/(1000*60*60);
        Random rand=new Random();
        for(int i=1;i<26;i++){
            //点赞收藏，肯定是新增，这个新增的积分值
            this.redisTemplate.opsForZSet().add(Constants.HOUR_KEY+hour,String.valueOf((char)(96+i)),rand.nextInt(10));

        }
    }
    //刷新当天的统计数据
    public void refreshDay(){
        long hour=System.currentTimeMillis()/(1000*60*60);
        List<String> otherKeys=new ArrayList<>();
        //算出近24小时内的key
        for(int i=1;i<23;i++){
            String key= Constants.HOUR_KEY+(hour-i);
            otherKeys.add(key);
        }
        //把当前时间key，并且把后推23小时，接近23小时，求出并集，存入Constants,DAY_KEY中
        //redis,ZUNIONSTORE，求并集
        this.redisTemplate.opsForZSet().unionAndStore(Constants.HOUR_KEY+hour,otherKeys,Constants.DAY_KEY);

        //设置当天的key,40天过期，不然历史数据浪费内存
        for(int i=0;i<24;i++){
            String key=Constants.HOUR_KEY+(hour-i);
            this.redisTemplate.expire(key,40, TimeUnit.DAYS);
        }
        log.info("天刷新完成");
    }

    //定时1小时合并，统计天，周，月的排行榜
    public void  refreshData(){
        while (true){
            this.refreshDay();
            this.refreshWeek();
            this.refreshMonth();
            try{
                Thread.sleep(1000*60*60);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }


    //刷新单周的统计数据
    public void refreshWeek(){
        long hour=System.currentTimeMillis()/(1000*60*60);
        List<String> otherKeys=new ArrayList<>();
        for(int i=1;i<24*7-1;i++){
            String key= Constants.HOUR_KEY+(hour-i);
            otherKeys.add(key);
        }
        this.redisTemplate.opsForZSet().unionAndStore(Constants.HOUR_KEY+hour,otherKeys,Constants.WEEK_KEY);
        log.info("周刷新完成");
    }
    //刷新单月的统计数据
    public void refreshMonth(){
        long hour=System.currentTimeMillis()/(1000*60*60);
        List<String> otherKeys=new ArrayList<>();
        //算出30天内的key
        for(int i=1;i<24*30-1;i++){
            String key= Constants.HOUR_KEY+(hour-i);
            otherKeys.add(key);
        }
        //把当前的时间key,往后推24*30个小时，近24*30小时，求出并集存入Constants.MONTH_KEY中
        this.redisTemplate.opsForZSet().unionAndStore(Constants.HOUR_KEY+hour,otherKeys,Constants.MONTH_KEY);
        log.info("月刷新完成...");
    }
     //定时5s，模拟微博的热度刷新(比如说模拟点赞，收藏，评论)
    public void  refreshDataHour(){
        while (true){
            this.refreshDataHour();
            try{
                Thread.sleep(5000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}
